package com.wang.project.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wang.project.annotation.AuthCheck;
import com.wang.project.common.BaseResponse;
import com.wang.project.common.ErrorCode;
import com.wang.project.common.ResultUtils;
import com.wang.project.constant.UserConstant;
import com.wang.project.exception.BusinessException;
import com.wang.project.model.dto.InterfacePay.InterfaceAddNumRequest;
import com.wang.project.model.dto.InterfacePay.OrderInterfaceDeleteRequest;
import com.wang.project.model.dto.InterfacePay.OrderInterfaceQueryRequest;
import com.wang.project.model.dto.InterfacePay.OrderInterfaceUpdateRequest;
import com.wang.project.model.entity.UserInterfacePayment;
import com.wang.project.model.vo.pay.OrderInterfaceAddNumVO;
import com.wang.project.pay.zfb.ZFBPreorderActionVO;
import com.wang.project.service.UserInterfacePaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/payment/interface")
public class UserInterfacePaymentController {

    @Resource
    private UserInterfacePaymentService userInterfacePaymentService;


    /**
     * 增加接口调用次数
     *
     * @param interfaceAddNumRequest
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/addInterfaceNum")
    public BaseResponse<ZFBPreorderActionVO> addInterfaceNum(@RequestBody InterfaceAddNumRequest interfaceAddNumRequest, HttpServletRequest request, HttpServletResponse response) {
        ZFBPreorderActionVO payToRefreshKey = userInterfacePaymentService.addInterfaceNum(interfaceAddNumRequest, request, response);
        return ResultUtils.success(payToRefreshKey);
    }


    // 下面是订单操作

    /**
     * 删除订单
     *
     * @param orderInterfaceDeleteRequest
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUser(@RequestBody OrderInterfaceDeleteRequest orderInterfaceDeleteRequest) {
        if (orderInterfaceDeleteRequest == null || orderInterfaceDeleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        return ResultUtils.success(userInterfacePaymentService.removeById(orderInterfaceDeleteRequest.getId()));
    }

    /**
     * 更新用户
     *
     * @param orderRefreshKeyUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUser(@RequestBody OrderInterfaceUpdateRequest orderRefreshKeyUpdateRequest) {
        if (orderRefreshKeyUpdateRequest == null || orderRefreshKeyUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        UserInterfacePayment userInterfacePayment = new UserInterfacePayment();
        BeanUtils.copyProperties(orderRefreshKeyUpdateRequest, userInterfacePayment);
        return ResultUtils.success(userInterfacePaymentService.updateById(userInterfacePayment));
    }

    /**
     * 根据 id 获取用户
     *
     * @param id 订单id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<UserInterfacePayment> getUserById(@RequestParam("id") Long id) {
        if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(userInterfacePaymentService.getById(id));
    }

    /**
     * 查询所有payment订单信息
     * 只能管理员才能获取订单信息
     *
     * @param orderInterfaceQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/queryList")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<List<OrderInterfaceAddNumVO>> listPayment(OrderInterfaceQueryRequest orderInterfaceQueryRequest, HttpServletRequest request) {
        if (orderInterfaceQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(userInterfacePaymentService.getPaymentListQuery(orderInterfaceQueryRequest, request));
    }

    /**
     * 分页获取用户列表
     *
     * @param orderInterfaceQueryRequest
     * @param request
     * @return
     */
    @GetMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<OrderInterfaceAddNumVO>> listPaymentByPage(OrderInterfaceQueryRequest orderInterfaceQueryRequest, HttpServletRequest request) {
        Page<OrderInterfaceAddNumVO> orderInterfaceAddNumVOPage = userInterfacePaymentService.listUserByPage(orderInterfaceQueryRequest, request);
        return ResultUtils.success(orderInterfaceAddNumVOPage);
    }
}
